package com.tianci.appstore;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;

import com.coocaa.x.framework.utils.LogUtils;
import com.tianci.appstore.controller.AppStoreZoneController;
import com.tianci.appstore.ui.data.SkyAppDetailData;
import com.tianci.appstore.ui.data.SkyAppDetailData.AppStatus;
import com.tianci.appstore.ui.data.SkyListAppData;
import com.tianci.appstore.ui.gamezone.GameZoneView;
import com.tianci.appstore.ui.gamezone.ZoneDataListener;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class GameZoneActivity extends SkyBaseActivity implements ZoneDataListener {
    protected GameZoneView contentView;
    private static final int UPDATE_VIEW = 1;
    private static final int UPDATE_STATUS = 2;
    private String bgUrl = null;
    private boolean isOneKeySetup = false;
    private List<SkyListAppData> dataList = null;
    String zoneId = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        LogUtils.d("game_time", "1. onCreate");
        contentView = new GameZoneView(this);
        LogUtils.d("game_time", "2. new GameZoneView finished");
        setContentView(contentView);
        LogUtils.d("game_time", "3. setContentView finished");
//        test();
        Intent intent = getIntent();
        Uri uri = intent.getData();
        if (uri != null){
            zoneId = uri.getQueryParameter("id");
        }else{
            zoneId = intent.getStringExtra("id");
        }
        LogUtils.d("game_time", "4. zoneId set");
        AppStoreZoneController.getInstance().init();
        AppStoreZoneController.getInstance().setZoneDataListener(this);
        LogUtils.d("game_time", "5. Controller setZoneDataListener");
        AppStoreZoneController.getInstance().getZoneInfo(Integer.parseInt(zoneId));
        LogUtils.d("game_time", "6. Controller getZoneInfo");
    }

    @Override
    protected Map<String, String> getPageStartParams() {
        HashMap<String,String> map = new HashMap<String,String>();
        map.put("zone_id",zoneId);
        return map;
    }

    private void test() {
        String[] testUrls = {
                "http://img2.imgtn.bdimg.com/it/u=3327770994,896938376&fm=23&gp=0.jpg",
                "http://img0.imgtn.bdimg.com/it/u=1370673618,951730014&fm=23&gp=0.jpg",
                "http://img2.imgtn.bdimg.com/it/u=2837474629,2171706581&fm=23&gp=0.jpg",
                "http://img2.imgtn.bdimg.com/it/u=1005809849,465971277&fm=23&gp=0.jpg",
                "http://img2.imgtn.bdimg.com/it/u=3317603295,3167190105&fm=90&gp=0.jpg",
                "http://img5.imgtn.bdimg.com/it/u=3825024720,2522486715&fm=23&gp=0.jpg",
                "http://img2.shejigao.redocn.com/attachments/images/201301/20130125/20130123_4feb22a13f885bf27429T5KWouhma1tJ.jpg",
                "http://img1.imgtn.bdimg.com/it/u=1087281206,947259179&fm=23&gp=0.jpg",
                "http://img4.imgtn.bdimg.com/it/u=2475540289,477058497&fm=23&gp=0.jpg",
                "http://img3.imgtn.bdimg.com/it/u=1084857366,1403808285&fm=23&gp=0.jpg",
                "http://img0.imgtn.bdimg.com/it/u=205401514,572915216&fm=23&gp=0.jpg",
                "http://img1.imgtn.bdimg.com/it/u=4121157238,3057715939&fm=23&gp=0.jpg",
                "http://img1.imgtn.bdimg.com/it/u=3854598332,2913712611&fm=23&gp=0.jpg",
                "http://img5.imgtn.bdimg.com/it/u=209419481,3378178878&fm=23&gp=0.jpg",
                "http://img5.imgtn.bdimg.com/it/u=905506099,2483219194&fm=23&gp=0.jpg",
                "http://f.hiphotos.baidu.com/image/h%3D360/sign=b5a07765ae4bd1131bcdb1346aafa488/7af40ad162d9f2d32a19b7f9aaec8a136327cc98.jpg",
                "http://g.hiphotos.baidu.com/image/h%3D360/sign=e0450cf5fe1f4134ff370378151f95c1/c995d143ad4bd11335ab262459afa40f4bfb0545.jpg",
                "http://img.article.pchome.net/00/37/67/67/pic_lib/wm/hbtt_05.jpg",
                "http://g.10086.cn/Upload/attachment/201004/14/b4b4699e3d1a93879f9bfe03d79f65a4.jpg",
                "http://img6.3lian.com/c23/desk2/10/07/56.jpg"};

        List<SkyListAppData> dataList = new ArrayList<SkyListAppData>();
        for (int i = 0; i < 20; i++) {
            SkyListAppData data = new SkyListAppData("id", i + "测试标题---", testUrls[i], "", "");
            dataList.add(data);
        }
        contentView.update(
//                "http://www.79n.cn/uploads/allimg/130115/1-130115213425.jpg"
                "http://www.79n.cn/uploads/allimg/121220/1-121220110320.jpg"
                , dataList, isOneKeySetup);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // TODO Auto-generated method stub
        if (contentView.processKeyDown(keyCode, event))
            return true;
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public void onZoneDataLoaded(String zoneCover, List<SkyListAppData> appList, boolean isOnKeySetup) {
        // TODO Auto-generated method stub
        this.bgUrl = zoneCover;
        this.dataList = appList;
        this.isOneKeySetup = isOnKeySetup;
        mainHandler.sendEmptyMessage(UPDATE_VIEW);
    }

    @Override
    public void onAppStatusChanged(String pkg, AppStatus status) {
        // TODO Auto-generated method stub
        Message msg = mainHandler.obtainMessage(UPDATE_STATUS);
        Bundle bundle = new Bundle();
        bundle.putString("pkgName", pkg);
        msg.setData(bundle);
        msg.obj = status;
        mainHandler.sendMessage(msg);
    }

    private Handler mainHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            // TODO Auto-generated method stub
            switch (msg.what) {
                case UPDATE_VIEW:
                    LogUtils.d("game_time", "    receive update view message");
                    contentView.update(bgUrl, dataList, isOneKeySetup);
                    LogUtils.d("game_time", "    contentView update data finished");
                    break;
                case UPDATE_STATUS:
                    contentView.updateAppStatus(msg.getData().getString("pkgName"), (SkyAppDetailData.AppStatus) msg.obj);
                    break;
            }
            super.handleMessage(msg);
        }
    };

    @Override
    public String getName() {
        // TODO Auto-generated method stub
        return "应用圈游戏专区界面";
    }
}
